<template>
	<view class="page">
		<view class="title">输入短信验证码</view>
		<view class="info">验证码已发送至：{{ tel }}</view>
		<view class="flex-box input-box flex-between">
			<input type="number" @input="change(1)" v-model="code1" :focus='focusIndex == 1?true:false'/>
			<input type="number" @input="change(2)" v-model="code2" :focus='focusIndex == 2?true:false'/>
			<input type="number" @input="change(3)" v-model="code3" :focus='focusIndex == 3?true:false'/>
			<input type="number" @input="change(4)" v-model="code4" :focus='focusIndex == 4?true:false'/>
		</view>
		<view class="countDown">{{ countDownTxt }}</view>
		<view class="countDown btn" v-if='showBtn' @click="login">重新发送</view>
	</view>
</template>

<script>
	var that,timer=null;
	export default {
		data() {
			return {
				focusIndex: 1,
				code1:'',
				code2:'',
				code3:'',
				code4:'',
				countDownTxt:'',
				username:'',
				password:'',
				uid:'',
				tel:'',
				showBtn: false
			};
		},
		onLoad(e) {
			that = this;
			if(e.uid){
				that.uid = e.uid
				that.username = e.uname
				that.password = e.upass
				that.tel = e.tel
				that.setInterval()
			}
			// #ifdef H5
			this.$common.H5HideShare()
			// #endif
		},
		methods:{
			setInterval(){
				let count = 60;
				that.showBtn = false
				timer = setInterval(() => {
					if (count > 0) {
						count -= 1;
						that.countDownTxt = count + 's后可重新发送'
					} else {
						that.countDownTxt = ' ';
						that.showBtn = true;
						clearInterval(timer);
					}
				}, 1000)
			},
			login(){
				that.$http.post({
					url: '/singleSaleManageApi/manageLogin',
					data:{
						uname: that.username,
						upass: that.password,
					}
				}).then(res => {
					if(res.code == 200){
						that.setInterval()
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
			change(index){
				that.focusIndex = 5
				if(index == 1 && that.code1){
					that.focusIndex = 2
				}
				if(index == 2 && that.code2){
					that.focusIndex = 3
				}
				if(index == 3 && that.code3){
					that.focusIndex = 4
				}
				if(index == 4 && that.code4){
					that.focusIndex = 5
				}
				var code ='' + that.code1 + that.code2 + that.code3 + that.code4
				if(code.length == 4){
					that.$http.post({
						url: '/singleSaleManageApi/bindOpenid',
						data:{
							uid: that.uid,
							code: code,
						}
					}).then(res => {
						if(res.code == 200){
							clearInterval(timer);
							uni.setStorageSync('store_id',res.data)
							uni.reLaunch({
								url:'../index/index'
							})
						}else{
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.page{
		background-color: #FFFFFF;
		padding: 50rpx 78rpx;
		.title{
			font-size: 48rpx;
		}
		.info{
			color: #666666;
			font-size: 28rpx;
			margin: 30rpx 0 40rpx;
		}
		.input-box{
			input{
				border-bottom: 1px solid #979797;
				width: 118rpx;
				font-size: 48rpx;
				font-weight: 600;
				text-align: center;
				line-height: 66rpx;
			}
		}
		.countDown{
			margin-top: 40rpx;
			color: #CCCCCC;
			font-size: 28rpx;
		}
		.btn{
			color: $uni-color-primary;
		}
	}

</style>
